package com.tommy.ddp.swapHandle
{
	import com.tommy.ddp.GridDataManager;
	import com.tommy.ddp.MainManager;
	import com.tommy.ddp.box.BoxFactory;
	import com.tommy.ddp.box.DdpBox;
	import com.tommy.ddp.definition.BoxDisposeAnimationTime;
	import com.tommy.ddp.definition.BoxExeType;
	import com.tommy.ddp.events.DisposeVoHandlerEvent;
	import com.tommy.ddp.finder.CreateNewVo;
	import com.tommy.ddp.finder.DisposeVo;
	import com.tommy.ddp.finder.FinderVo;
	import com.tommy.ddp.grid.DdpGrid;
	import com.tommy.ddp.manager.DisposeBoxManager;
	
	import flash.events.EventDispatcher;
	import flash.utils.setTimeout;

	/**
	 * 处理消除数据
	 * @author hongxuan
	 * 
	 */	
	public class DisposeVoHandler extends EventDispatcher
	{
		private var _disposeVo:DisposeVo;
		private var subDisposeVo:SubDisposeVo;
		
		public function DisposeVoHandler()
		{
		}
		public function execute(vo:DisposeVo):void
		{
			_disposeVo = vo;
			var finderList:Vector.<FinderVo> = vo.finderVoList;
			
			var addtionVect:Vector.<DdpBox> = new Vector.<DdpBox>();
			for each(var i:FinderVo in finderList)
			{
				var a_v:Vector.<DdpBox> = i.disposeBox();
				for each(var b:DdpBox in a_v)
				{
					if(addtionVect.indexOf(b) == -1)
						addtionVect.push(b);
				}
			}
			//处理影响到的格子
			var gridList:Vector.<DdpGrid> = vo.pounchGrids;
			GridDataManager.punchGrids(gridList);
			
			//
			if(_disposeVo.createNewVo)
			{
				var newVo:CreateNewVo = _disposeVo.createNewVo;
				var box:DdpBox = BoxFactory.createSpecifiedBox(newVo.point.x,newVo.point.y,newVo.colorType,newVo.exeType);
				MainManager.game.container.addChild(box);
			}
			//
			if(addtionVect.length > 0)
			{
				subDisposeVo = new SubDisposeVo();
				subDisposeVo.type = BoxExeType.NONE;
				subDisposeVo.vect = addtionVect;
			}
			setTimeout(checkIsOver,BoxDisposeAnimationTime.getTime(_disposeVo.type));
		}
		
		public function dispose():void
		{
			_disposeVo = null;
			subDisposeVo = null;
		}
		
		public function executeSub(vo:SubDisposeVo):void
		{
			var subVo:SubDisposeVo = vo;
			subDisposeVo = null;
			
			var vect:Vector.<DdpBox> = subVo.vect;
			var addtionVect:Vector.<DdpBox> = new Vector.<DdpBox>();
			for each(var i:DdpBox in vect)
			{
				i.isInDisposeList = true;
			}
			for each(i in vect)
			{
				//addtionVect = addtionVect.concat(DisposeBoxManager.addDisposeBox(i));
				var a_v:Vector.<DdpBox> = DisposeBoxManager.addDisposeBox(i);
				for each(var b:DdpBox in a_v)
				{
					if(addtionVect.indexOf(b) == -1)
						addtionVect.push(b);
				}
			}
			//
			if(addtionVect.length > 0)
			{
				subDisposeVo = new SubDisposeVo();
				subDisposeVo.type = BoxExeType.NONE;
				subDisposeVo.vect = addtionVect;
			}
			setTimeout(checkIsOver,BoxDisposeAnimationTime.getTime(subVo.type));
		}
		
		
		private function checkIsOver():void
		{
			if(subDisposeVo)
			{
				trace("有附加消除……继续执行");
				executeSub(subDisposeVo);
			}
			else
			{
				trace("已经没有附加消除……回合结束");
				dispatchEvent(new DisposeVoHandlerEvent(DisposeVoHandlerEvent.COMPLETE));
			}
		}
	}
}